package com.feature50.makeover;

import com.feature50.makeover.filthy.GraphicsUtilities;
import com.feature50.makeover.filthy.ShadowRenderer;
import com.feature50.makeover.utils.SwingUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;

public class BouncyLightweightPopup extends JPanel {
    private JComponent contents;

    // to support the "pop" effect, "built" works a little differently than it does in other components;
    // the build-out isn't "done" until buildDone is true
    private float built = 1f;
    private boolean buildDone = false;

    public static final float SIZE_REDUCER = 0.1f;
    private static final float SIZE_AUGMENTER = 1.125f;


    private BufferedImage img;

    private ShadowRenderer shadow = new ShadowRenderer();
    private BufferedImage imgShadow;

    public BouncyLightweightPopup(JComponent contents) {
        this.contents = contents;
        add(contents);
        setOpaque(false);
    }

    @Override
    public void doLayout() {
        int width = getWidth() - (int) (getWidth() * SIZE_REDUCER) - shadow.getSize();
        int height = getHeight() - (int) (getHeight() * SIZE_REDUCER) - shadow.getSize();

        int x = (getWidth() / 2) - (width / 2);
        int y = (getHeight() / 2) - (height / 2);

        contents.setBounds(x, y, width, height);
    }

    protected void paintChildren(Graphics g) {
        if (!buildDone) {
            if ((img == null) || (img.getHeight() != contents.getHeight())) {
                img = GraphicsUtilities.createCompatibleImage(contents.getWidth(), contents.getHeight());
                Graphics2D g2d = img.createGraphics();
                contents.print(g2d);
                g2d.dispose();
            }
            int cw = Math.max((int) (getWidth() * built), 1);
            int ch = Math.max((int) (getHeight() * built), 1);

            int x = (getWidth() / 2) - (cw / 2);
            int y = (getHeight() / 2) - (ch / 2);

            g.drawImage(img, x, y, cw, ch, null);
        } else {
            BufferedImage img = GraphicsUtilities.createCompatibleTranslucentImage(getWidth(), getHeight());
            Graphics2D g2d = img.createGraphics();
            super.paintChildren(g2d);
            g2d.dispose();

            if (imgShadow == null) imgShadow = shadow.createShadow(img);
            g.drawImage(imgShadow, 0, 0, null);
            g.drawImage(img, 0, 0, null);
        }
    }

    public float getBuilt() {
        return built;
    }

    public void setBuilt(float built) {
        this.built = built;
    }

    public boolean isBuildDone() {
        return buildDone;
    }

    public void setBuildDone(boolean buildDone) {
        this.buildDone = buildDone;
    }

    public Dimension getPreferredSize() {
        return SwingUtils.newDimension(contents.getPreferredSize(), SIZE_AUGMENTER);
    }

    public Dimension getMinimumSize() {
        return SwingUtils.newDimension(contents.getMinimumSize(), SIZE_AUGMENTER);
    }

    public Dimension getMaximumSize() {
        return SwingUtils.newDimension(contents.getMaximumSize(), SIZE_AUGMENTER);
    }
}
